package main;

import service.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 游戏主窗口类
 */
public class GameFrame extends JFrame implements ActionListener {
    // -------------------- 数据设计 --------------------
    private GamePanel gamePanel;
    private JMenuBar menuBar;
    private JMenu gameMenu, helpMenu;
    private JMenuItem newGame, selectLevel, pause, exit, about;

    private GameLogicService gameLogicService;
    // -------------------- 数据设计 --------------------

    /**
     * 负责人: 无忌
     * 功能: 游戏窗口构造函数
     * 初始化窗口组件
     * 设置菜单栏
     * 创建游戏面板
     * 参数: void
     * 返回值: void
     */
    public GameFrame() {
        initializeComponents();
        setupWindow();
        createServices();
        setVisible(true);
    }

    /**
     * 负责人:无忌
     * 功能: 初始化窗口组件
     * 参数: void
     * 返回值: void
     */
    private void initializeComponents() {
        // 创建菜单栏
        menuBar = new JMenuBar();
        gameMenu = new JMenu("游戏(G)");
        helpMenu = new JMenu("帮助(H)");

        // 创建菜单项
        newGame = new JMenuItem("新游戏(N)");
        selectLevel = new JMenuItem("选择关卡(S)");
        pause = new JMenuItem("暂停(P)");
        exit = new JMenuItem("退出(X)");
        about = new JMenuItem("关于(A)");

        // 设置快捷键
        newGame.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
        selectLevel.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
        pause.setAccelerator(KeyStroke.getKeyStroke("P"));
        exit.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));

        // 添加事件监听器
        newGame.addActionListener(this);
        selectLevel.addActionListener(this);
        pause.addActionListener(this);
        exit.addActionListener(this);
        about.addActionListener(this);

        // 组装菜单
        gameMenu.add(newGame);
        gameMenu.add(selectLevel);
        gameMenu.addSeparator();
        gameMenu.add(pause);
        gameMenu.addSeparator();
        gameMenu.add(exit);
        helpMenu.add(about);

        menuBar.add(gameMenu);
        menuBar.add(helpMenu);
        setJMenuBar(menuBar);

        // 创建游戏面板
        gamePanel = new GamePanel();
        add(gamePanel);
    }

    /**
     * 负责人: 无忌
     * 功能: 设置窗口属性
     * 参数: void
     * 返回值: void
     */
    private void setupWindow() {
        setTitle("坦克大战");
        setSize(1200, 950); // 考虑菜单栏高度
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
    }


    private void createServices() {
        gameLogicService = new GameLogicService();
    }

    /**
     * 负责人: 无忌
     * 功能: 菜单事件处理
     * 参数:
     *   e: 事件对象
     * 返回值: void
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == newGame) {
            gameLogicService.startNewGame();
            gamePanel.requestFocusInWindow();

        } else if (e.getSource() == selectLevel) {
            String levelStr = JOptionPane.showInputDialog(this,
                    "请输入关卡数 (1-8):", "选择关卡", JOptionPane.QUESTION_MESSAGE);
            try {
                int level = Integer.parseInt(levelStr);
                if (level >= 1 && level <= 8) {
                    data.GameRuntimeData.getInstance().setCurrentGrade(level);
                    gameLogicService.startNewGame();
                    gamePanel.requestFocusInWindow();
                } else {
                    JOptionPane.showMessageDialog(this, "关卡数必须在1-8之间！");
                }
            } catch (NumberFormatException ex) {
                if (levelStr != null) { // 用户没有取消
                    JOptionPane.showMessageDialog(this, "请输入有效的数字！");
                }
            }

        } else if (e.getSource() == pause) {
            data.GameRuntimeData runtimeData = data.GameRuntimeData.getInstance();
            if (runtimeData.getGameState() == 1) {
                runtimeData.setGameState(2);
                runtimeData.setGameRunning(false);
                pause.setText("继续(P)");
            } else if (runtimeData.getGameState() == 2) {
                runtimeData.setGameState(1);
                runtimeData.setGameRunning(true);
                pause.setText("暂停(P)");
            }
            gamePanel.requestFocusInWindow();

        } else if (e.getSource() == exit) {
            int option = JOptionPane.showConfirmDialog(this,
                    "确定要退出游戏吗？", "确认退出", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                System.exit(0);
            }

        } else if (e.getSource() == about) {
            JOptionPane.showMessageDialog(this,
                    "坦克大战 v1.0\n\n" +
                            "开发团队：卷王组\n" +
                            "架构设计：数据-服务-视图分离\n" +
                            "操作说明：\n" +
                            "WASD或方向键：移动坦克\n" +
                            "空格键：发射子弹\n" +
                            "P键：暂停/继续游戏\n" +
                            "Ctrl+N：新游戏\n" +
                            "Ctrl+S：选择关卡",
                    "关于坦克大战", JOptionPane.INFORMATION_MESSAGE);
        }
    }
}